
import ResManager from "../manager/ResManager";
import { UICF, UIID } from "./UIConst";
import { UIView } from "./UIView";


export class UIManager {
    
    private static _instance: UIManager = null;
    public static get instance(): UIManager {
        if (this._instance == null) {
            this._instance = new UIManager();
        }
        return this._instance;
    }
    public init(){
        this.rootNode =cc.find('UIRoot');
        cc.game.addPersistRootNode(this.rootNode);
        for (let index = 0; index < this.rootNode.children.length; index++) {
            const element = this.rootNode.children[index];
            this.layers[index] = element;
        }
    }
    public layers:cc.Node[] = [];
    private rootNode: cc.Node = null;
    private zIndex = 200;
    private UICache: Map<UIID, UIView> = new Map<UIID, UIView>();
    private stack: Array<UIView> = [];
    public async loadUI(uiId: number) {
        await this.getOrCreateUI(uiId);
    }

    private async getOrCreateUI(uiId: number,) {
        let uiView: UIView = this.UICache.get(uiId);
        if (uiView) {
            return uiView;
        }
        let uiPath = UICF[uiId].prefab;
        if (!uiPath) {
            console.log(`getOrCreateUI ${uiId} faile, uiPath == null!`);
            return;
        }
        
        let node = await ResManager.instance.loadPrefab(uiPath);
        if (!node) {
            console.log(`getOrCreateUI ${uiId} faile, prefab conf not found!`);
            return;
        }
        uiView = node.getComponent(UIView);
        if (null == uiView) {
            console.log(`getOrCreateUI getComponent ${uiId} faile, prefab don't have UIView Component`);
            node.destroy();
            return;
        }
        this.UICache.set(uiId,uiView);
        uiView.onCreate && uiView.onCreate();
        let level = uiView.level;
        this.layers[level].addChild(node);
        return uiView;
    }
    
    public getUI(uiId: number): UIView {
        return this.UICache.get(uiId);
    }
    /** 打开界面并添加到界面栈中 */
    public async open(uiId: number, ...uiArgs) {
        let uiView = await this.getOrCreateUI(uiId);
        if (!uiView) {
            console.error(`无法打开：${UIID[uiId]}`);
            return;
        }
        uiView.UIid = uiId;
        this.stack.push(uiView);
        uiView.node.zIndex= this.zIndex;
        this.zIndex++;
        uiView.onOpen(...uiArgs);
        if (uiView.content) {
            uiView.isOpen = true;
            uiView.content.scale = 0;
            cc.tween(uiView.content).to(0.2,{scale:0.9},{
                easing: "sineIn",
            }).start();
        }else{
            uiView.isOpen = true;
            
        }
    }
    /**
     * 关闭当前界面
     * @param uiView 要关闭的界面
     */
    public close(uiView: UIView | UIID) {
        if (uiView instanceof UIView) {
            if (!uiView) {
                console.error('uiView为空');
                return;
            }
            let index = this.stack.indexOf(uiView);
            if (index != -1) {
                this.stack.splice(index, 1);
            }
            uiView.isOpen = false;
            uiView.onClose();
        } else {
            uiView = this.getUI(uiView);
            let index = this.stack.indexOf(uiView);
            if (index != -1) {
                this.stack.splice(index, 1);
            }
            uiView.isOpen = false;
            uiView.onClose();
        }

    }
    public closeAll(){
        this.UICache.forEach((value, key, map) => {
            value.isOpen = false;
            //ResManager.instance.release(UICF[value.UIid].prefab);
        });
        this.stack.length = 0;
    }
    public destroyAll() {
        this.UICache.forEach((value, key, map) => {
            value.node.destroy();
            //ResManager.instance.release(UICF[value.UIid].prefab);
        });
        this.stack = null;
        this.UICache = null;
    }
    public destroy(uiView: UIView) {
        uiView.node.destroy();
        this.UICache.delete(uiView.UIid);
        let index = this.stack.indexOf(uiView);
        if (index != -1)
            this.stack.splice(index, 1);
        //ResManager.instance.release(UICF[uiView.UIid].prefab);
    }

    public showToast(text: string) {
        this.open(UIID.UIToast, text);
    }
   

}